//
//  HHBadge.m
//  HHChatKit
//
//  Created by Henry on 2021/5/10.
//

#import "HHBadge.h"

#define     FONT_TITLE          [UIFont systemFontOfSize:12]
#define     MAX_HEIGHT          20.0f
#define     MIN_HEIGHT          8.0f
#define     MAGRIN              5.0f

@interface HHBadge ()

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation HHBadge

#pragma mark - # Class Methods
+ (CGSize)badgeSizeWithValue:(NSString *)value {
    return [self badgeSizeWithValue:value font:FONT_TITLE];
}

+ (CGSize)badgeSizeWithValue:(NSString *)value font:(UIFont *)font {
    return [self badgeSizeWithValue:value font:font maxHeight:MAX_HEIGHT minHeight:MIN_HEIGHT];
}

+ (CGSize)badgeSizeWithValue:(NSString *)value font:(UIFont *)font maxHeight:(CGFloat)maxHeight minHeight:(CGFloat)minHeight {
    if (!value || [value isEqualToString:@"0"] || [value isEqualToString:@""]) {
        return CGSizeZero;
    }
    if ([value intValue] == 0) {
        return CGSizeZero;
    }
    if ([value isEqualToString:@"-1"] || [value intValue] == -1) {
        return CGSizeMake(minHeight, minHeight);
    }
    CGSize size = [value sizeWithAttributes:@{NSFontAttributeName:font}];
    CGFloat width = size.width + MAGRIN * 2;
    width = MAX(width, maxHeight);
    return CGSizeMake(ceil(width), maxHeight);
}

#pragma mark - # Init
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.titleLabel];
        
        // 默认属性设置
        [self setBackgroundColor:[UIColor redColor]];
        [self setTitleColor:[UIColor whiteColor]];
        [self setTitleFont:FONT_TITLE];
        [self setMaxHeight:MAX_HEIGHT];
        [self setMinHeight:MIN_HEIGHT];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // 更新圆角大小
    self.layer.cornerRadius = self.frame.size.height / 2.0;
    if (!self.badgeValue) {
        self.titleLabel.frame = CGRectZero;
        return;
    }
    self.titleLabel.frame = self.bounds;
    
}

#pragma mark - Public Methods
- (void)setBadgeValue:(id)badgeValue {
    _badgeValue = badgeValue;
    self.titleLabel.text = badgeValue;
    [self.titleLabel sizeToFit];
    [self p_resetFrame];
}

- (void)setTitleFont:(UIFont *)titleFont {
    _titleFont = titleFont;
    self.titleLabel.font = titleFont;
    [self p_resetFrame];
}

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    self.titleLabel.textColor = titleColor;
}

- (void)setMaxHeight:(CGFloat)maxHeight {
    _maxHeight = maxHeight;
    [self p_resetFrame];
}

- (void)setMinHeight:(CGFloat)minHeight {
    _minHeight = minHeight;
    [self p_resetFrame];
}

#pragma mark - Private Methods
- (void)p_resetFrame {
    CGSize size = [HHBadge badgeSizeWithValue:self.badgeValue font:self.titleFont maxHeight:self.maxHeight minHeight:self.minHeight];
    if (size.width == 0) {
        self.hidden = YES;
        size.width = self.minHeight;
        size.height = self.minHeight;
    } else {
        self.hidden = NO;
    }
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, size.width, size.height);
    [self setNeedsDisplay];
}

#pragma mark - Getters
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    }
    return _titleLabel;
}

@end
